草庐IT

html - 将 Wiki 标记转换为 HTML 的最简单方法是什么?

全部标签

ruby - 在 Ruby 中将字符串转换为十六进制

我正在尝试使用Ruby将二进制文件转换为十六进制。目前我有以下内容:File.open(out_name,'w')do|f|f.puts"constunsignedintmodFileSize=#{data.length};"f.puts"constcharmodFile[]={"first_line=truedata.bytes.each_slice(15)do|a|line=a.map{|b|",#{b}"}.joiniffirst_linef.putsline[1..-1]elsef.putslineendfirst_line=falseendf.puts"};"end这是以下代

ruby - 我们如何在不同的 Ruby 类之间复制单例方法?

我试图定义一个带有方法的类,以及一个缺少这些方法的类,然后允许后一个类的对象从前一个类的实例“学习”这些方法。这是我的尝试(Ruby1.9.2)-当我尝试更改lambda绑定(bind)中“self”的值时,它中断了(在注释为“BREAKS!”的行处)。如果您能想出如何解决这个问题-我很想知道。classSkillattr_accessor:nameattr_accessor:techniquedefinitialize(name,&technique_proc)@name=name@technique=lambda(&proc)endendclassPersonattr_access

ruby-on-rails - 为什么 do/end 和 {} 不总是等价的?

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:RubyblockandunparenthesizedargumentsWhatisthedifferenceorvalueoftheseblockcodingstylesinRuby?我一直认为以下只是同一件事的两种表达方式:[1,2,3].collect{|i|i*2}[1,2,3].collectdo|i|i*2end但是我在我的一个ERB模板中发现了一些奇怪的行为,这两种语法似乎在做两件不同的事情。这段代码效果很好:m))}}%>但是当我将其重写为:m))endend%>...我最终得到了我的@men

Java 有 FindBugs。 Ruby 的等价物是什么?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:AutomaticcodequalitytoolforRuby?Java有FindBugs™。Ruby的等价物是什么?

ruby-on-rails - 仅当特定实例变量具有值时,如何 stub 实例方法?

我有一个对象MyObject:classMyObjectdefinitialize(options={})@stat_to_load=options[:stat_to_load]||'test'enddefresults[]endend仅当stat_to_load="times"时,我才想stubresults方法。我怎样才能做到这一点?我试过了:MyObject.any_instance.stubs(:initialize).with({:stat_to_load=>"times"}).stubs(:results).returns(["klala"])但它不起作用。有什么想法吗?

ruby-on-rails - 从帮助器规范中 stub 一个帮助器方法

我正在构建Rails应用程序并使用RSpec制定测试。我为我正在创建的名为current_link_to的方法编写了测试。此方法应该检查当前页面是否对应于我传递给它的路径,并将current类添加到生成的链接中,以防它匹配。这是规范:require"spec_helper"describeApplicationHelperdodescribe"#current_link_to"dolet(:name){"Products"}let(:path){products_path}let(:rendered){current_link_to(name,path)}context"whenthe

ruby - 在多个线程中引用类方法会导致自动加载循环依赖崩溃

代码:threads=[]Thread.abort_on_exception=truebegin#throwexceptionsinthreadssowecanseethemthreadseputs"EXCEPTION:#{e.inspect}"puts"MESSAGE:#{e.message}"end崩溃:.rvm/gems/ruby-2.1.3@req/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:478:inload_missing_constant':自动加载常量MyClass时检测到循环依赖稍加研究后,

ruby - 用于简单数组操作的慢速 Ruby

我按照Cormen的“算法导论”中的伪代码,在Ruby中创建了简单的插入排序实现:defsort_insert(array)(1...array.length).eachdo|item_index|key=array[item_index]i=item_index-1whilei>=0&&array[i]>keydoarray[i+1]=array[i]i-=1endarray[i+1]=keyendarrayend它有效,但执行速度非常慢。对于约20k个元素的数组array=((0..10_000).to_a*2).shuffle,排序大约需要20秒。我只测量这个方法调用的时间,没有

ruby - 检查是否调用了类方法

我有以下模块和类:moduleMyModuledefself.includedbasebase.extend(ClassMethods)endmoduleClassMethodsattr_reader:config#thismethodMUSTbecalledbyeveryclasswhichincludesMyModuledefconfigure&block@config={}block.call(@config)ifblockendendendclassAincludeMyModuleconfiguredo|config|#dosthwiththeconfigendendclass

ruby - 为什么 Ruby 的 splat 在组合数组时比使用 + 组合数组慢?

我大胆猜测将一个数组拼成另一个数组比将两个数组加在一起更快,但经过快速基准测试后我发现我错了。我假设解释器只会将splat转换为数组文字,而不必每次都对其调用+方法。那么,为什么+比splat更快?我使用了这个基准代码:deftest(trials=1000)head=[1,2,3]tail=100.times.to_at=Time.now.to_ftrials.timesdo|i|a=[head,*tail]endputs"splatdonein#{Time.now.to_f-t}"t=Time.now.to_ftrials.timesdo|i|a=head+tailendputs"